// ============================== 导入依赖 ============================== //

import CORE from '@root/【CORE】'
import JSEncrypt from 'jsencrypt'

const RSA = new JSEncrypt()
const secretTag = 'ib_platform_pub'
let begin = 0
let end = 0

// ============================== 导出加密工具 ============================== //

export default {
  /**
   * RSA 加密
   *
   * @param {String} str 明文串
   */
  async rsa(str) {
    let now = Date.now()
    if (now > end) {
      const resp = await CORE.post('获取公钥', '/ib/openapi/v1/auth/getPublicKey', { secretTag })
      begin = now = Date.now()
      end = resp.expireAt - resp.notBefore + begin
      RSA.setPublicKey(resp.publicKey)
    }
    return RSA.encrypt(`${now}${str}`)
  },
}
